<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <form action="http://ajax-api.itheima.net/api/data" method="post">
    <input class="username" type="text" name="username" placeholder="请输入用户名" />
    <br />
    <input type="text" name="food" placeholder="请输入喜欢的食物" />
    <br />
    <input type="textarea" name="sign" placeholder="请输入个性签名" />
    <br />
    <button type="submit">提交</button>
  </form>
  <!-- 导入了 在线的js文件 -->
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script src="./03-源代码/00.knowledges/lib/form-serialize.js"></script>
  <script>
    //阻止默认行为
    // document.querySelector('button').onclick = (e) => { e.preventDefault() }
    document.querySelector('form').onsubmit = (e) => {
      e.preventDefault()
      //serialize作用:一次性获取form表单中的内容
      const form = document.querySelector('form')
      const data = serialize(form, { hash: true })
      console.log(data);
      //用axios发起请求
      axios({
        url: 'http://ajax-api.itheima.net/api/data',
        method: 'post',
        data: data
      }).then((res) => console.log(res))
    }
  </script>
</body>

</html>